package net.sudot.chess.business.controller.admin.system;

import net.sudot.chess.business.dto.WechatMessageDTO;
import net.sudot.chess.config.WechatBotProperties;
import net.sudot.chess.exception.SystemException;
import net.sudot.commons.security.CsrfInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * 微信机器人登陆管理
 *
 * @author tangjialin on 2018-03-04
 */
@RestController("adminWechatBotController")
@RequestMapping("/admin/wechat-bot")
public class WechatBotController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private volatile Map<String, String> pushData;
    @Resource
    private WechatBotProperties wechatBotProperties;
    @Resource
    private RestTemplate restTemplate;

    /**
     * 检测微信机器人登录人状态
     *
     * @return
     */
    @GetMapping("/alive")
    public boolean alive() {
        try {
            ResponseEntity<Boolean> post = restTemplate.postForEntity(wechatBotProperties.getDomain() + "/alive", null, Boolean.class);
            return Boolean.TRUE.equals(post.getBody());
        } catch (HttpClientErrorException e) {
            // 忽略
        }
        return false;
    }

    /**
     * 登录机器人
     *
     * @return
     */
    @GetMapping("/login")
    public Map<String, String> login() {
        restTemplate.postForObject(wechatBotProperties.getDomain() + "/login", null, Void.class);
        while (true) {
            try { Thread.sleep(1000L); } catch (InterruptedException e) { throw new SystemException("机器人登录二维码获取失败"); }
            if (pushData != null) {
                Map<String, String> pushData = this.pushData;
                this.pushData = null;
                return pushData;
            }
        }
    }

    /**
     * 退出机器人登录状态并关闭机器人
     */
    @GetMapping("/logout")
    @ResponseStatus(code = HttpStatus.NO_CONTENT)
    public void logout() {
        try {
            restTemplate.postForObject(wechatBotProperties.getDomain() + "/logout", null, Void.class);
        } catch (HttpClientErrorException e) {
            // 忽略
        }
    }

    /**
     * 发送信息
     *
     * @param message 信息内容
     * @return
     */
    @PostMapping("/message")
    public String message(@RequestBody @Valid WechatMessageDTO message) {
        try {
            return restTemplate.postForObject(wechatBotProperties.getDomain() + "/message", message, String.class);
        } catch (HttpClientErrorException e) {
            // 忽略
        }
        return null;
    }

    /**
     * 收到登录二维码
     *
     * @param body
     */
    @CsrfInterceptor.Ignore
    @PostMapping("/qrcode")
    @ResponseStatus(code = HttpStatus.NO_CONTENT)
    public void qrcode(@RequestBody Map<String, String> body) {
        logger.info("收到登录二维码:{}", body);
        pushData = body;
    }

}
